using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class SudokuGrid : UIBehaviour
{
    // Refereces
    public GridBackground background;
    public GridNumbers numbers;
    public GridLine separators;
    public GridSelector selector;
    
    // Attributes
    [Space]
    [Range(0, 100)]
    public int width = 9;
    [Range(0, 100)]
    public int height = 9;

    private RectTransform m_rectTransform;
    public RectTransform rectTransform
    {
        get
        {
            if (m_rectTransform == null) m_rectTransform = GetComponent<RectTransform>();
            return m_rectTransform;
        }
    }

    private GridHelper sodoku;

    #region Logic
    public void NewBoard()
    {
        sodoku.Reset();
        //var grid = sodoku.Generator.Grid;
        //for (int i = 0; i < grid.Length; i++)
        //{
        //    if (grid[i] == 0) continue;
        //    var x = i / width;
        //    var y = i % width;
        //    numbers.ShowCell(x, y, grid[i]);
        //    background.SetState(x, y, GridBackground.ColorState.Immutable);
        //}

        // the row of ui and the col of ui
        for (int row = 0; row < GridGenerator.GRID_SIZE; row++)
        {
            int gridRow = GridGenerator.GRID_SIZE - row - 1;
            for (int col = 0; col < GridGenerator.GRID_SIZE; col++)
            {
                var num = sodoku.GetGridNum(gridRow, col);
                if (num == 0) continue;
                numbers.ShowCell(col, row, num);
                background.SetState(col, row, GridBackground.ColorState.Immutable);
            }
        }
        background.UpdatePresent();
    }

    public void SetNumber(int x, int y, int num)
    {
        var result = sodoku.PlaceNumber(num, y, x);
        if (result == null)
        {
            
        }
        else
        {

        }

        //Debug.Log($"TODO: Create set {num} request at ({x}, {y})");
    }

    #endregion

    #region Events
    public void OnCellClick(int x, int y)
    {
        if (background.stateCells[x, y] == GridBackground.ColorState.Immutable) return;

        var centerX = x - Mathf.RoundToInt(width / 2);
        var centerY = y - Mathf.RoundToInt(height / 2);

        var cellWidth = rectTransform.rect.width / width;
        var cellHeight = rectTransform.rect.height / height;
        
        selector.SetNumber(sodoku.GetGridNum(y, x));
        selector.rectTransform.anchoredPosition = new Vector2(cellWidth * centerX, cellHeight * centerY);
    }

    protected override void Awake()
    {
        sodoku = new GridHelper();
    }

    protected override void Start()
    {
        selector.rectTransform.sizeDelta = new Vector2(rectTransform.rect.width / width - 0.1f, rectTransform.rect.height / height - 0.1f);
        NewBoard();
    }

#if UNITY_EDITOR
    protected override void OnValidate()
    {
        SetAttributes();
    }
#endif

    protected override void OnRectTransformDimensionsChange()
    {
        separators.SetVerticesDirty();
        background.SetVerticesDirty();
    }

    private void SetAttributes()
    {
        numbers.width = separators.width = background.width = width;
        numbers.height = separators.height = background.height = height;

        separators.SetVerticesDirty();
        background.ResetStateArray();
        background.SetVerticesDirty();
    }
    #endregion
}
